package com.fleafinder.entity;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.os.Parcel;
import android.os.Parcelable;

public class Market implements Parcelable {
	/**
	 * 
	 */
	private static final long serialVersionUID = -820424667369046189L;
	private int id;
	private String name;
	private String marketTypeName;
	private String feeType;
	private String road;
	private String zip;
	private String city;
	private String dateFrom;
	private String dateTo;
	private String startAt;
	private String endAt;
	private String stande;
	private String description;
	
	//private long dateFromNum;
	//private long dateToNum;
	
	private Date dateFromObject;
	private Date dateToObject;
	
	private double longtitude;
	private double latitude;
	
	//if the longtitude & latitude of this Marker have been loaded
	//then this variable is TRUE, otherwise it is FALSE
	private boolean isLoadedLocation = false;
	
	//if this market is meant to be shown on map then this variable is TRUE
	//otherwise this variable is FALSE
	private boolean isShowOnMap = false;
	
	private boolean isBusiness = false;
	
	private Contact contact;
	private MarketType marketType;
	
	private Date loadedTime;
	
	private List<Category> categories;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public MarketType getMarketType() {
		return marketType;
	}

	public void setMarketType(MarketType marketType) {
		this.marketType = marketType;
	}

	public String getDateFrom() {
		return dateFrom;
	}

	public void setDateFrom(String dateFrom) {
		this.dateFrom = dateFrom;
	}

	public String getDateTo() {
		return dateTo;
	}

	public void setDateTo(String dateTo) {
		this.dateTo = dateTo;
	}

	public String getStande() {
		return stande;
	}

	public void setStande(String stande) {
		this.stande = stande;
	}

	public String getStartAt() {
		return startAt;
	}

	public void setStartAt(String startAt) {
		this.startAt = startAt;
	}

	public String getEndAt() {
		return endAt;
	}

	public void setEndAt(String endAt) {
		this.endAt = endAt;
	}

	public String getZip() {
		return zip;
	}

	public void setZip(String zip) {
		this.zip = zip;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getRoad() {
		return road;
	}

	public void setRoad(String road) {
		this.road = road;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Contact getContact() {
		return contact;
	}

	public void setContact(Contact contact) {
		this.contact = contact;
	}

	public List<Category> getCategories() {
		return categories;
	}

	public void setCategories(List<Category> categories) {
		this.categories = categories;
	}

	public String getFeeType() {
		return feeType;
	}

	public void setFeeType(String feeType) {
		this.feeType = feeType;
	}

//	public long getDateFromNum() {
//		return dateFromNum;
//	}
//
//	public void setDateFromNum(long dateFromNum) {
//		this.dateFromNum = dateFromNum;
//	}
//
//	public long getDateToNum() {
//		return dateToNum;
//	}
//
//	public void setDateToNum(long dateToNum) {
//		this.dateToNum = dateToNum;
//	}

	public double getLongitude() {
		return longtitude;
	}

	public void setLongitude(double longtitude) {
		this.longtitude = longtitude;
	}

	public double getLatitude() {
		return latitude;
	}

	public void setLatitude(double latitude) {
		this.latitude = latitude;
	}

	public boolean isLoadedLocation() {
		return isLoadedLocation;
	}

	public void setLoadedLocation(boolean isLoadedLocation) {
		this.isLoadedLocation = isLoadedLocation;
	}

	public boolean isShowOnMap() {
		return isShowOnMap;
	}

	public void setShowOnMap(boolean isShowOnMap) {
		this.isShowOnMap = isShowOnMap;
	}

	public String getMarketTypeName() {
		return marketTypeName;
	}

	public void setMarketTypeName(String marketTypeName) {
		this.marketTypeName = marketTypeName;
	}

	public Date getDateFromObject() {
		return dateFromObject;
	}

	public void setDateFromObject(Date dateFromObject) {
		this.dateFromObject = dateFromObject;
	}

	public Date getDateToObject() {
		return dateToObject;
	}

	public void setDateToObject(Date dateToObject) {
		this.dateToObject = dateToObject;
	}

	public boolean isBusiness() {
		return isBusiness;
	}

	public void setBusiness(boolean isBusiness) {
		this.isBusiness = isBusiness;
	}

	public Date getLoadedTime() {
		return loadedTime;
	}

	public void setLoadedTime(Date loadedTime) {
		this.loadedTime = loadedTime;
	}
	
	public Market(){
		
	}
	
	/*
	 * private int id;
	private String name;
	private String marketTypeName;
	private String feeType;
	private String road;
	private String zip;
	private String city;
	private String dateFrom;
	private String dateTo;
	private String startAt;
	private String endAt;
	private String stande;
	private String description;
	
	//private long dateFromNum;
	//private long dateToNum;
	
	private Date dateFromObject;
	private Date dateToObject;
	
	private double longtitude;
	private double latitude;
	
	//if the longtitude & latitude of this Marker have been loaded
	//then this variable is TRUE, otherwise it is FALSE
	private boolean isLoadedLocation = false;
	
	//if this market is meant to be shown on map then this variable is TRUE
	//otherwise this variable is FALSE
	private boolean isShowOnMap = false;
	
	private boolean isBusiness = false;
	
	private Contact contact;
	private MarketType marketType;
	
	private Date loadedTime;
	
	private List<Category> categories;
	 */
	public Market(Parcel parcel){
		readFromParcel(parcel);
	}
	
	public static final Parcelable.Creator<Market> CREATOR = new Parcelable.Creator<Market>() {

		@Override
		public Market createFromParcel(Parcel source) {
			return new Market(source);
		}

		@Override
		public Market[] newArray(int size) {
			return new Market[size];
		}
	};

	@Override
	public int describeContents() {
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeInt(id);
		dest.writeString(name);
		dest.writeString(marketTypeName);
		dest.writeString(feeType);
		dest.writeString(road);
		dest.writeString(zip);
		dest.writeString(city);
		dest.writeString(dateFrom);
		dest.writeString(dateTo);
		dest.writeString(startAt);
		dest.writeString(endAt);
		dest.writeString(stande);
		dest.writeString(description);
		dest.writeLong(dateFromObject.getTime());
		dest.writeLong(dateToObject.getTime());
		dest.writeDouble(longtitude);
		dest.writeDouble(latitude);
		dest.writeByte((byte) (isLoadedLocation ? 1 : 0));
		dest.writeByte((byte) (isShowOnMap ? 1: 0));
		dest.writeByte((byte) (isBusiness ? 1 : 0));
		
		dest.writeByte((byte) (contact == null ? 1 : 0));
		
		if(contact != null){
			dest.writeString(contact.getEmail() == null ? "" : contact.getEmail());
			dest.writeString(contact.getFacebook() == null ? "" : contact.getFacebook());
			dest.writeString(contact.getHomepage() == null ? "" : contact.getHomepage());
			dest.writeString(contact.getName() == null ? "" : contact.getName());
			dest.writeString(contact.getPhone() == null ? "" : contact.getPhone());
			dest.writeString(contact.getTwitter() == null ? "" : contact.getTwitter());
		}
		
		dest.writeByte((byte) (marketType == null ? 1 : 0));
		if(marketType != null){
			dest.writeInt(marketType.getMarketId());
			dest.writeString(marketType.getMarketTypeName());
		}
		
		dest.writeLong(loadedTime.getTime());
		
		StringBuffer sb = new StringBuffer();
		for(int i = categories.size() - 1; i > 0; --i){
			sb.append(categories.get(i).getId()).append(",").append(categories.get(i).getName()).append(",");
		}
		sb.append(categories.get(0).getId()).append(",").append(categories.get(0).getName());
		dest.writeString(sb.toString());
	}

	public void readFromParcel(Parcel parcel){
		id = parcel.readInt();
		name = parcel.readString();
		marketTypeName = parcel.readString();
		feeType = parcel.readString();
		road = parcel.readString();
		zip = parcel.readString();
		city = parcel.readString();
		dateFrom = parcel.readString();
		dateTo = parcel.readString();
		startAt = parcel.readString();
		endAt = parcel.readString();
		stande = parcel.readString();
		description = parcel.readString();
		dateFromObject = new Date(parcel.readLong());
		dateToObject = new Date(parcel.readLong());
		longtitude = parcel.readDouble();
		latitude = parcel.readDouble();
		isLoadedLocation = parcel.readByte() > 0 ? true : false;
		isShowOnMap = parcel.readByte() > 0 ? true : false;
		isBusiness = parcel.readByte() > 0 ? true : false;

		boolean contactWasNull = parcel.readByte() > 0 ? true : false;
		if (!contactWasNull) {
			contact = new Contact();
			contact.setEmail(parcel.readString());
			contact.setFacebook(parcel.readString());
			contact.setHomepage(parcel.readString());
			contact.setName(parcel.readString());
			contact.setPhone(parcel.readString());
			contact.setTwitter(parcel.readString());
		}
		
		boolean marketTypeWasNull = parcel.readByte() > 0 ? true : false;
		if(!marketTypeWasNull){
			marketType = new MarketType();
			marketType.setMarketId(parcel.readInt());
			marketType.setMarketTypeName(parcel.readString());
		}
		
		loadedTime = new Date(parcel.readLong());
		
		categories = new ArrayList<Category>();
		String categoriesAsString = parcel.readString();
		String[] categoriesAsArray = categoriesAsString.split(",");
		for(int i = 0; i < categoriesAsArray.length; i += 2){
			Category c = new Category();
			c.setId(Integer.parseInt(categoriesAsArray[i]));
			c.setName(categoriesAsArray[i + 1]);
			categories.add(c);
		}
	}
}
